package com.ruoyi.web.controller.market;

import com.ruoyi.market.domain.MarketActivity;
import com.ruoyi.market.service.IMarketActivityService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 营销活动Controller
 *
 * @author ruoyi
 * @date 2023-01-26
 */
@RestController
@RequestMapping("/market/activity")
public class MarketActivityController extends BaseController
{
    @Autowired
    private IMarketActivityService marketActivityService;

    /**
     * 查询营销活动列表
     */
    @PreAuthorize("@ss.hasPermi('activty:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(MarketActivity marketActivity)
    {
        startPage();
        List<MarketActivity> list = marketActivityService.selectMarketActivityList(marketActivity);
        return getDataTable(list);
    }

    /**
     * 导出营销活动列表
     */
    @PreAuthorize("@ss.hasPermi('activty:activity:export')")
    @Log(title = "营销活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MarketActivity marketActivity)
    {
        List<MarketActivity> list = marketActivityService.selectMarketActivityList(marketActivity);
        ExcelUtil<MarketActivity> util = new ExcelUtil<MarketActivity>(MarketActivity.class);
        util.exportExcel(response, list, "营销活动数据");
    }

    /**
     * 获取营销活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('activty:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(marketActivityService.getById(id));
    }

    /**
     * 新增营销活动
     */
    @PreAuthorize("@ss.hasPermi('activty:activity:add')")
    @Log(title = "营销活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MarketActivity marketActivity)
    {
        return toAjax(marketActivityService.save(marketActivity));
    }

    /**
     * 修改营销活动
     */
    @PreAuthorize("@ss.hasPermi('activty:activity:edit')")
    @Log(title = "营销活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MarketActivity marketActivity)
    {
        return toAjax(marketActivityService.updateById(marketActivity));
    }

    /**
     * 删除营销活动
     */
    @PreAuthorize("@ss.hasPermi('activty:activity:remove')")
    @Log(title = "营销活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(marketActivityService.removeBatchByIds(Arrays.asList(ids)));
    }
}
